using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using SharePoint.ImportExport.Core;

using SharePoint.ImportExport.ExportSPS2001.ExportData;

namespace SharePoint.ImportExport.ExportSPS2001.cmdSPS2001Extractor
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class mainApp
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			if(args.Length<2)
			{
				Console.WriteLine("SharePoint Portal Server 2001 Extractor Version {0}", version);
				Console.WriteLine("Incorrect parameters!");
				Console.WriteLine("Usage");
				Console.WriteLine("");
				Console.WriteLine("-configFile ConfigFile.xml");
				Console.WriteLine("or");
				Console.WriteLine("-Workspace [URL to SPS2001 workspace]");
				Console.WriteLine("-Directory [Path To ExportDirectory]");
				Console.WriteLine("-Owner [Domain account of Site owner]");
				Console.WriteLine("-SiteDefinition [Name of the site definition to use]"); 
				Console.WriteLine("-SpsRootFolder [Name of the SPS 2001 folder to extract]"); 
				Console.WriteLine("Optional");
				Console.WriteLine("-Email [Email of the SiteOwner]");
				Console.WriteLine("-ProfileToUse [Name of DocumentProfile to use if all documents are exported into the same library");
				Console.WriteLine("-SiteCollectionRoot [Name of the SiteCollection root e.g. Sites]"); 
				Console.WriteLine("-ExportType AllInSameFolder|AFolderPerProfile"); 
				Console.WriteLine("-MigrateVersions True/False"); 
				Console.ReadLine();
				return;
			}
			@params p=new @params();

			for(int i=0;i<args.GetLength(0);i+=2)
			{
				string lowerarg=args[i];
				string data=args[i+1];
				switch(lowerarg.ToLower())
				{
					case "-configfile":
						{
						if(File.Exists(data))
						{
							try
							{
								// Create an instance of the XmlSerializer specifying type.
								XmlSerializer serializer = new XmlSerializer(typeof(@params));
								// Create a TextReader to read the file. 
								FileStream fs = new FileStream(data, FileMode.Open);
								TextReader reader = new StreamReader(fs);
	      
								// Use the Deserialize method to restore the object's state.
								p = (@params) serializer.Deserialize(reader);

								fs.Close();

							}
							catch(Exception Ex)
							{
								Console.WriteLine("Could not open the configuration file: "+data);
								return;
							}
						}
						else
						{
						}
						break;
						}
					case "workspace":
					{
						p.WorkSpace=data;
						break;
					}
					case "directory":
					{
						p.Directory=data;
						break;
					}
					case "owner":
					{
						p.Owner=data;
						break;
					}
					case "sitedefinition":
					{
						p.SiteDefinitionName=data;
						break;
					}
					case "email":
					{
						p.Email=data;
						break;
					}
					case "profiletouse":
					{
						p.ProfileToUse=data;	
						break;
					}
					case "sitecollectionroot":
					{
						p.SiteContainerRoot=data;	
						break;
					}
					case "spsrootfolder":
					{
						p.RootFolder=data;	
						break;
					}
					case "exporttype":
					{
						if(data.ToLower()=="allinsamefolder")
							p.AllInSameFolder=true.ToString();
						else
							p.AllInSameFolder=false.ToString();
						break;
					}
					case "migrateversions":
					{
						p.MigrateVersions=data;
						break;
					}
				}
			}
			SharePoint.ImportExport.ExportSPS2001.ExportData.Handler exporter=new Handler();
			exporter.WorkspaceName=p.WorkSpace;
			exporter.ExportDirectoryName=p.Directory;
			exporter.ExportFolderName="/Documents";
			exporter.ExportVersions=bool.Parse(p.MigrateVersions);
			exporter.Owner=p.Owner;
			exporter.Email=p.Email;
			exporter.SiteDefinitionName=p.SiteDefinitionName;
			exporter.RootContainerName=p.RootFolder;
			exporter.ImportSiteType=SiteType.Site;
			if(bool.Parse(p.AllInSameFolder))
				exporter.FolderCreationOptions=FolderCreationOptionsEnum.AllInSameFolder;
			else
				exporter.FolderCreationOptions=FolderCreationOptionsEnum.AFolderPerProfile;
			Logger.Init("2001Export.log",ErrorLevel.Information);
			Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Log(ErrorLevel.Progress, "SharePoint Portal Server 2001 Extractor Version "+version+" starting...");

			if(exporter.Process())
			{
			}


		}

		private const string version = "0.3";
		private static void IncomingEventLogData(object sender, SPEventArgs e)
		{
			string logArgs = DateTime.Now.ToLongTimeString()+ ":  " + e.Message;
			Console.WriteLine(logArgs);

		}

	}
}
